$().ready(function() {
	validateRule();
	$("#types").val(sessionStorage.getItem('types'));

	let objectDate = {
		'moduleType':1
	};

	$.ajax({
		cache : true,
		type : "POST",
		url:'/jz-jinzhuan-disposition-module/getModuleList',
		data : JSON.stringify(objectDate),// 你的formid
		async : false,
		headers: {
			'Content-Type':'application/json;charset=UTF-8'
		},
		error : function(request) {
			parent.layer.alert("Connection error");
		},

		success : function(data) {
			let str = '';
			if (data.code == 0) {
				$.each(data.data,function (k,v){
					if( k == 0 ){
						str += `<option value="">请选择</option>`;
					}
					str += `<option value="${v.id}">${v.moduleName}</option>`;
				})
				$("#templet").html(str);
			} else {
				parent.layer.alert(data.msg)
			}

		}
	})

	let example = xmSelect.render({
		el: '#refereeABox',
		autoRow: true,
		name: '',
		// max:3,
		initValue: [] ,
		data() {
			return []
		},
		// maxMethod(seles, item) {
		// 	layer.msg(`最多只能选择3个`)
		// },
		on: function (data) {
			let arr = [];
			data.arr.map(function (v){
				arr.push(v.id);
			})
			$(".examferrA").val( arr.join(',').replace(/,/g,';;') );
		}
	})

	let example2 = xmSelect.render({
		el: '#encipher',
		autoRow: true,
		name: '',
		// max:3,
		initValue: [] ,
		data() {
			return []
		},
		// maxMethod(seles, item) {
		// 	layer.msg(`最多只能选择3个`)
		// },
		on: function (data) {
			let arr = [];
			data.arr.map(function (v){
				arr.push(v.id);
			})
			$(".encipher").val( arr.join(',').replace(/,/g,';;') );
		}
	})

	$("#competitionType").change(function (){
		changeSelefut($(this).val());
	})
	changeSelefut($("#competitionType").val())
	function changeSelefut(data){

		if(data == 1) $("#referrA").show()
		else $("#referrA").hide()

		if( data == 0 ){
			layer.msg('请选择竞赛类型，不然无法选择裁判');
			example.update({
				data: [],
				autoRow: true,
				initValue: []
			});
			example2.update({
				data: [],
				autoRow: true,
				initValue: []
			})
			return false;
		}

		let map = {
			track: 'A',
			competitionType: data
		}
		$.ajax({
			cache: true,
			type: "post",
			url: "/jzweb/examinationBatchCom/getUserReferee",
			headers: {
				'Content-Type':'application/json;charset=UTF-8'
			},
			data: JSON.stringify(map),
			async: false,
			error: function (request) {
				parent.layer.alert("Connection error");
			},
			success: function (data) {

				if (data.code == 0) {
					var format = data.data;
					let examferrA = $(".examferrA").val();
					examferrA = examferrA.split(';;');
					format.forEach(function (v, k) {
						v.selected = true;
						v.value = v.id
					})
					example.update({
						data: format,
						autoRow: true
					})
				}
			}
		})

		map = {
			track: 'D',
			competitionType: data
		}
		$.ajax({
			cache: true,
			type: "post",
			url: "/jzweb/examinationBatchCom/getUserReferee",
			headers: {
				'Content-Type':'application/json;charset=UTF-8'
			},
			data: JSON.stringify(map),
			async: false,
			error: function (request) {
				parent.layer.alert("Connection error");
			},
			success: function (data) {

				if (data.code == 0) {
					var format = data.data;
					let encipher = $(".encipher").val();
					encipher = encipher.split(';;');
					format.forEach(function (v, k) {
						v.selected = true;
						v.value = v.id
					})
					example2.update({
						data: format,
						autoRow: true
					})

				}
			}
		})
	}

});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {

	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/examinationBatch/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			cbatchId : {
				required : true
			},
			passMark : {
				required : true
			},
			totalScore : {
				required : true
			},
			type:{
				required : true
			},
			startTime:{
				required : true
			},
			endTime:{
				required : true
			},
			competitionName:{
				required : true
			},
			batchType:{
				required : true
			},
			competitionType:{
				required : true
			},
			modileId:{
				required : true
			},
		},
		messages : {
			cbatchId : {
				required : icon + "请输入考试批次名称"
			},
			name : {
				required : icon + "请输入考试批次名称"
			},
			passMark : {
				required : icon + "请输入合格分"
			},
			totalScore : {
				required : icon + "请输入总分"
			},
			type : {
				required : icon + "请选择考试类型"
			},
			startTime:{
				required : icon + "请添加竞赛开始时间"
			},
			endTime:{
				required : icon + "请添加竞赛结束时间"
			},
			competitionName:{
				required : icon + "请添加竞赛编号"
			},
			batchType:{
				required : icon + "请选择批次类型"
			},
			competitionType:{
				required : icon + "请选择竞赛类型，不然无法选择裁判"
			},
			modileId:{
				required : icon + "请选择模板"
			}
		}
	})
}

layui.use('laydate', function() {
	var laydate = layui.laydate;
	laydate.render({
		elem: '#startTime'
		,type: 'datetime'
	});
	laydate.render({
		elem: '#endTime'
		,type: 'datetime'
	});
})